package com.circle.rest.modular.sys.controller;

import com.circle.rest.core.common.model.SuccessResponseData;
import com.circle.rest.core.gmap.GMapTool;
import com.circle.rest.core.gmap.model.District;
import com.circle.rest.core.gmap.model.IpLocation;
import com.circle.rest.core.jwt.JwtUtil;
import com.circle.rest.core.manager.webScoket.WebSocketServer;
import com.circle.rest.core.util.RedisUtil;
import com.circle.rest.modular.sys.model.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: RestfulApi
 * @description: 测试
 * @author: 赵航
 * @create: 2019-04-02 16:02
 **/
@RestController
@RequestMapping("/test")
public class TestController {
    /**
     * token过期时间
     *
     * @return
     */
    @RequestMapping(value = "/do1", method = RequestMethod.GET)
    public Object do1() {
        Map<String, Object> map = new HashMap<>();

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 获取当前的用户
        User currentUser = JwtUtil.getUser(request);

        long expire = RedisUtil.getExpire("JWT-TOKEN:" + currentUser.getId());
        map.put("redis过期时间", expire);
        Long expiration = JwtUtil.getExpiration();
        map.put("jwt  过期时间", expiration);
        return new SuccessResponseData(map);
    }

    /**
     * 有效token列表
     *
     * @return
     */
    @RequestMapping(value = "/do2", method = RequestMethod.GET)
    public Object do2() {
        List<String> list = new ArrayList<>(RedisUtil.select("JWT-TOKEN:"));
        return new SuccessResponseData(list);
    }

    /**
     * socket
     *
     * @return
     */
    @RequestMapping(value = "/do3", method = RequestMethod.GET)
    public Object do3(String msg, String userId) throws IOException {
        WebSocketServer.sendInfo(msg, userId);
        return new SuccessResponseData();
    }

    @RequestMapping(value = "/do4", method = RequestMethod.GET)
    public Object proTest(String keywords) {
        District select = GMapTool.getDistricts(keywords);
        IpLocation ipLocation = GMapTool.getIpLocation("103.63.154.55");
        return new SuccessResponseData(select);
    }
}
